﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
//using MixScript.DBService;
using MixScript.Appl.Security.Crypto;
//using MixScript.BLL.ApplConfig;

namespace MixScript.BLL.ApplAdmin
{
    public class cUserManagement : cApplAdmin
    {
        private DataTable c_dt;
        private cAES_Cryptography c_oCrypt;
        private string upass;
        public string Upass
        {
            get { return upass; }
            set { upass = value; }
        }
        private string peopleid;
        public string Peopleid
        {
            get { return peopleid; }
            set { peopleid = value; }
        }

        public cUserManagement(string sUID)
        {
            // oDB = new cDBServer();

            

        }

        public cUserManagement()
        {
            //oDB = new cDBServer();
            c_dt = new DataTable();
            c_oCrypt = new cAES_Cryptography();
            
        }

        public int getErrorCount(string suid)
        {
            try
            {
                string sql = "select errorcount from sy_user where userid='" + suid + "'";
                return (int) Dbserver.ExecuteScalar(sql);                
                
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool incErrorCounter(string suid)
        {

            try
            {

                string sql = "update sy_user set errorCount=errorcount+1 where userid='" + suid + "'";
                if (Dbserver.openConn())
                {
                    Dbserver.ExecuteNonQuery(sql);
                    Dbserver.CommitAndCloseConn();
                    return true;
                }

                return false;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public string getUserPass(string suid)
        {
            try
            {
                string sql = "select userpassword from sy_user where userid='" + suid + "'";
                c_dt = Dbserver.ExecuteQuery(sql, "user");

                if (c_dt.Rows.Count > 0)
                {
                    return c_oCrypt.decryptString(c_dt.Rows[0]["userpassword"].ToString(), cAES_Cryptography.encKeys);
                }
                else
                {
                    return "";
                }

            }
            catch (Exception ex)
            {

                throw;
            }

        }
        public bool updatePasswoord(string suid, string spass)
        {
            try
            {
                string sql = "update sy_user set userpassword='" + c_oCrypt.encryptString(spass, cAES_Cryptography.encKeys) + "', passexpired='0' where userid='" + suid + "'";
                if (Dbserver.openConn() && isUSerExist(suid))
                {
                    return Dbserver.ExecuteNonQuery(sql);
                }
                else
                {
                    throw new Exception("Server Error");
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool ChangeUserPAss(string suid, string snewpass)
        {
            try
            {
                string oldpass = getUserPass(suid);
                if (snewpass != oldpass)
                {
                    return updatePasswoord(suid, snewpass);
                }
                else
                {
                    throw new Exception("Password tidak boleh sama dengan sebelumnya");
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool isUSerExist(string suid)
        {
            try
            {
                DataTable dt = new DataTable();
                return getUser(ref dt, suid);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        //public bool getUser(ref DataTable dt, string suid, string speopleid, bool passexpired, int errorcount);
        public bool getUser(ref DataTable dt, string suid)
        {
            try
            {
                //dt = new DataTable("userlogin")
                string sSQL = "select * from sy_user where 1=1 ";
                if (suid.Trim() != string.Empty)
                {
                    sSQL += "and userid='" + suid + "'";
                }

                dt = Dbserver.ExecuteQuery(sSQL, "userlogin");
                if (dt.Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        public override bool AddNewRowDB(out string msg1, string tablename, int[] indexgenfieldname, string[] genid, bool haveHistory, params object[] fldValue)
        {
            try
            {

                fldValue[1] = c_oCrypt.encryptString(fldValue[1].ToString(), cAES_Cryptography.encKeys);
                //string s = "";

                //if (Convert.ToBoolean(fldValue[3]) == true)
                //{
                //    s = "1";
                //}
                //else
                //{
                //    s = "0";
                //}
                return base.AddNewRowDB(out msg1, tablename, indexgenfieldname, genid, haveHistory, fldValue[0], fldValue[1], fldValue[2], fldValue[3],fldValue[4]);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public override bool EditRowDtDB(out string msg1, string tablename, string[] keyfieldname, object[] keyvalue, bool haveHistory, params object[] fldValue)
        {
            fldValue[1] = c_oCrypt.encryptString(fldValue[1].ToString(), cAES_Cryptography.encKeys);
            //string s = "";

            //if (Convert.ToBoolean(fldValue[3]) == true)
            //{
            //    s = "1";
            //}
            //else
            //{
            //    s = "0";
            //}

            return base.EditRowDtDB(out msg1, tablename, keyfieldname, keyvalue, haveHistory, fldValue[0], fldValue[1], fldValue[2], fldValue[3], fldValue[4]);
        }

        

    }
}
